package com.njust.controller;

import com.njust.common.ServerRespone;
import com.njust.service.TrainService;
import com.njust.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Slf4j
@Api("首页")
@RequestMapping("/")
public class RealDisplayController {

    @Autowired
    private TrainService trainService;

    @GetMapping("index/last")
    @ApiOperation("主页实时数据返回最新的数据")
    public ServerRespone indexLast() throws ParseException {
        Date date = new Date();
        //显示类型20180201000000
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd000000");
        String nowdate = simpleDateFormat.format(date);
        ServerRespone serverRespone=trainService.realShow(nowdate);
        return serverRespone;
    }

    @GetMapping("index/all")
    @ApiOperation("主页实时数据返回所有的数据")
    public ServerRespone indexALL() throws ParseException {
        Date date = new Date();
        //显示类型20180201000000
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd000000");
        String nowdate = simpleDateFormat.format(date);
        ServerRespone serverRespone=trainService.realShowall(nowdate);
        return serverRespone;
    }

    @GetMapping("/warn/{trainOnlyid}")
    @ApiOperation(value="报警弹出该车的所有故障")
    public ServerRespone warn(@PathVariable(name = "trainOnlyid")Long trainOnlyid){
        ServerRespone resultVO = trainService.warn(trainOnlyid);
        return resultVO;
    }

    @GetMapping("/{trainOnlyid}/traindetail")
    @ApiOperation(value="查看列车综合数据")
    public ServerRespone trainDetail(@PathVariable(name = "trainOnlyid")Long trainOnlyid){
        return trainService.showDetailByTrainOnlyId(trainOnlyid);
    }


    @GetMapping("/{trainOnlyid}/vibratedetail")
    @ApiOperation("振动轴温详细数据")
    public ServerRespone zhendongDetail(@PathVariable Long trainOnlyid){
            return trainService.zhendongDetail(trainOnlyid);
    }

    @GetMapping("/{trainOnlyid}/motordetail")
    @ApiOperation("电机温度详细数据")
    public ServerRespone motorDetail(@PathVariable Long trainOnlyid){
        return trainService.motorDetail(trainOnlyid);
    }

    @GetMapping("today/{trainDate}")
    @ApiOperation("运营结束显示最后十条记录")
    public ServerRespone todayLast10(@PathVariable String trainDate)throws ParseException{
        //String pre="20171016000000";
        Date firsttime = DateUtil.String2Date(trainDate);
        long time=firsttime.getTime()+86400000;
        Date lasttime=new Date(time);

        ServerRespone serverRespone = trainService.todayLast10(firsttime, lasttime);
        return serverRespone;
    }


    @GetMapping("deviceState/{warnRank}")
    @ApiOperation(value="三色灯控制")
    public ServerRespone deviceState(@PathVariable(name = "warnRank")Integer warnRank) throws IOException {
        ServerRespone resultVO = trainService.deviceState(warnRank);
        return resultVO;
    }


}
